package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeekhome.biz.WorkQualityStatisticsService;
import com.igeekhome.dao.WorkQualityStatisticsMapper;
import com.igeekhome.pojo.WorkQualityStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.NumberFormat;
import java.util.List;

/**
 * <p>
 * 工作质量统计 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/workQualityStatistics")
public class WorkQualityStatisticsController {

    @Autowired
    private WorkQualityStatisticsMapper workQualityStatisticsMapper;
    @Autowired
    @Resource
    private WorkQualityStatisticsService workQualityStatisticsService;

    @RequestMapping("/statistics")
    public String index(Model model, HttpSession session)
    {
        QueryWrapper queryWrapper = new QueryWrapper();
        NumberFormat num = NumberFormat.getPercentInstance();
        num.setMaximumIntegerDigits(3);
        num.setMaximumFractionDigits(2);
//        得到有效会话数
        int Count = workQualityStatisticsMapper.getAll(queryWrapper);
        session.setAttribute("Count",Count);
//        得到总会话消息数
        int messageCount = workQualityStatisticsMapper.getmessage(queryWrapper);
        session.setAttribute("messageCount",messageCount);
//        得到评价率
        double reViewRate = workQualityStatisticsMapper.getReView(queryWrapper);
        session.setAttribute("reViewRate", num.format(reViewRate));
//        得到客户数
        int customerCount = workQualityStatisticsMapper.getCustomer(queryWrapper);
        session.setAttribute("customerCount",customerCount);
//        得到问题解决率
        double solveRate = workQualityStatisticsMapper.getSolved(queryWrapper);
        session.setAttribute("solveRate",num.format(solveRate));
//        得到好评率
        double goodRate = workQualityStatisticsMapper.getgood(queryWrapper);
        session.setAttribute("goodRate",num.format(goodRate));

        model.addAttribute("main","统计数据总览");

        return "statistics";

    }

    //    @RequestMapping("/workloadstatistics")
//    public String index2(Model model, HttpSession session)
//    {
//
//        return "workloadstatistics";
//
//    }
    @RequestMapping("/workqualitystatistics")
    public String index3(Model model,@RequestParam("current") Integer current,@RequestParam("size") Integer size)
    {
//        QueryWrapper<WorkQualityStatistics> queryWrapper = new QueryWrapper<>();

//        WorkQualityStatistics workQualityStatistics = workQualityStatisticsService.getOne(queryWrapper);
//        WorkQualityStatistics workQualityStatistics1 = workQualityStatisticsService.getOne(queryWrapper);
        initWorkQualityStatisticsService(model, current, size);
        return "workqualitystatistics";

    }
    public void initWorkQualityStatisticsService(Model model,Integer current,Integer size)
    {

        IPage<WorkQualityStatistics> page=new Page<>(current,size);
        IPage<WorkQualityStatistics> page1 = this.workQualityStatisticsService.page(page);

        List<WorkQualityStatistics> list = page1.getRecords();
        long pageCount = page1.getPages();
        model.addAttribute("main","工作质量统计");

        model.addAttribute("list",list);
        model.addAttribute("pageCount",pageCount);
//        model.addAttribute("main","客服管理主页");
    }

    @RequestMapping("/del")
    public String del(Model model,WorkQualityStatistics workQualityStatistics,@RequestParam("current") Integer current,@RequestParam("size") Integer size)
    {
        this.workQualityStatisticsService.removeById(workQualityStatistics.getId());
        initWorkQualityStatisticsService(model,current,size);
        return "/workqualitystatistics";
    }

    @RequestMapping("/update")
    public String update(Model model, WorkQualityStatistics workQualityStatistics,@RequestParam("current") Integer current,@RequestParam("size") Integer size)
    {
        workQualityStatistics=this.workQualityStatisticsService.getById(workQualityStatistics.getId());
        //根据id将对象查询
//        model.addAttribute("main","客服修改");
        model.addAttribute("main","工作质量统计");

        model.addAttribute("current",current);
        model.addAttribute("size",size);
        model.addAttribute("workQualityStatistics",workQualityStatistics);
        return "updateworkqualitystatistics";
    }
    @RequestMapping("/updcom")
    public String updcom(Model model, WorkQualityStatistics workQualityStatistics,@RequestParam("current") Integer current,@RequestParam("size") Integer size)
    {
//        System.out.println("111");
        this.workQualityStatisticsService.saveOrUpdate(workQualityStatistics);
        initWorkQualityStatisticsService(model,current,size);
        return "workqualitystatistics";
    }
}

